#include <string>
#include<unistd.h>
#include<iostream>
using namespace std;
#include"init.hpp"
init a;

class Task
{
public:
    Task(int sockfd,char *clientip,uint16_t clientport)
        :sockfd_(sockfd),clientip_(clientip),clientport_(clientport)
    {}
    Task(){}
    void operator()()
    {
        char buffer[4096];
        while (true)
        {
            ssize_t n = read(sockfd_, buffer, sizeof(buffer));
            if (n > 0)
            {
                buffer[n] = 0;
                cout << "client say#" << buffer << endl;
                string echo_string=a.translation(buffer);
                cout<<" echo_string:"<<echo_string.c_str()<<endl;
                write(sockfd_, echo_string.c_str(), echo_string.size());
            }
            else if (n == 0)
            {
                lg(Info, "%s:%d quit, server close sockfd: %d", clientip_.c_str(), clientport_, sockfd_);
                break;
            }
            else
            {
                lg(Warning, "read error, sockfd: %d, client ip: %s, client port: %d", sockfd_, clientip_.c_str(), clientport_);
                break;
            }
        }
    }
    ~Task(){};
private:
    int sockfd_;
    string clientip_;
    uint16_t clientport_;
};